package com.expdatacloud.controller.iqc;


import com.expdatacloud.annotation.AutoLog;
import com.expdatacloud.dto.ApiResponse;
import com.expdatacloud.entity.IqcDetectionDetail;
import com.expdatacloud.mapper.IqcDetectionMapper;
import com.expdatacloud.service.IqcDetectionDetailService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * <p>
 * IQC-检测详情表 前端控制器
 * </p>
 *
 * @author jie
 * @since 2019-11-13
 */
@Controller
@RequestMapping("/iqcDetectionDetail")
public class IqcDetectionDetailController {

    @Autowired
    private IqcDetectionDetailService iqcDetectionDetailService;
    @Autowired
    private IqcDetectionMapper iqcDetectionMapper;

    /**
     * 根据收货单号sku查询
     */
    @GetMapping("/list")
    @ResponseBody
    public ApiResponse list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit,
                            IqcDetectionDetail iqcDetectionDetail) {
        PageHelper.startPage(page, limit);
        PageInfo<IqcDetectionDetail> pageInfo = new PageInfo<>(iqcDetectionDetailService.list(iqcDetectionDetail));
        return ApiResponse.ok(pageInfo.getTotal(), pageInfo.getList());
    }

    @AutoLog(title = "检验明细更新")
    @PostMapping(value = "/update")
    @ResponseBody
    public IqcDetectionDetail update(@RequestBody Object jsonStr, HttpSession session) {
        String users = session.getAttribute("users").toString();
        ObjectMapper mapper = new ObjectMapper();
        IqcDetectionDetail iqcCheckItemDetails = mapper.convertValue(jsonStr, IqcDetectionDetail.class);

        iqcDetectionDetailService.update(iqcCheckItemDetails, users);
        return iqcDetectionDetailService.calculateNumber(iqcCheckItemDetails.getReceiptKey(), iqcCheckItemDetails.getSku());

}

    /**
     * 提交自动填充不合格数量、拒收数量等
     */
    @GetMapping(value = "/fill")
    @ResponseBody
    public IqcDetectionDetail fill(String receiptKey, String sku) {
        return iqcDetectionDetailService.calculateNumber(receiptKey, sku);
    }

    /**
     * 更新 将检验明细中的拒收数量、验退数量清空 入库数量 = 待检数量
     */
    @GetMapping(value = "/cleanData")
    @ResponseBody
    public void cleanData(String receiptKey, String sku, String noOk, HttpSession session) {
        String user = session.getAttribute("users").toString();
        IqcDetectionDetail iqcDetectionDetail = new IqcDetectionDetail();
        iqcDetectionDetail.setReceiptKey(receiptKey);
        iqcDetectionDetail.setSku(sku);
        iqcDetectionDetailService.cleanData(iqcDetectionDetail, user, noOk);
    }
}

